//
//  CaoZuoDetailVC.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/9.
//

#import "CaoZuoDetailVC.h"
#import "CaoZuoDetailListCell.h"

@interface CaoZuoDetailVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic,strong)  UITableView       *  tableView;
@property (nonatomic, strong) NSMutableArray    *  dataAry;
@property (nonatomic, assign) NSInteger            pageIndex;
@property (nonatomic, assign) NSInteger            totalPage;

@end

@implementation CaoZuoDetailVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self layoutCustomNav:self.view title:[NSString stringWithFormat:@"%@的操作记录",self.model.nickName]];
}

- (void)viewDidLoad {
     [super viewDidLoad];
     self.view.backgroundColor=White_Color;
    [self dataAry];
    [self createTableView];
     MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         self.pageIndex=1;
         [self getCaoZuoJiLuList:NO];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        if (self.pageIndex > self.totalPage){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self getCaoZuoJiLuList:YES];
        }
    }];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.tableView.mj_header beginRefreshing];
}

-(void)createTableView{

    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, ScreenWidth, B_HEIGHT-LL_StatusBarAndNavigationBarHeight-LL_TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor=White_Color;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    [self.tableView registerNib:[UINib nibWithNibName:[CaoZuoDetailListCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[CaoZuoDetailListCell reuseIdentifier]];
    [self.view addSubview:self.tableView];
    CGRect frame = CGRectMake(0, 0, 0, 0.1);
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:frame];
}

#pragma mark - ------------------ DZNEmptyData ------------------
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"No_order_icon"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无数据";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15.0f],
                                 NSForegroundColorAttributeName:gray666_color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    if (!self.tableView.mj_header.isRefreshing && !self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer resetNoMoreData];
        self.pageIndex=1;
        [self getCaoZuoJiLuList:NO];
    }
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -50;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataAry.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CaoZuoDetailListCell *cell = [tableView dequeueReusableCellWithIdentifier:[CaoZuoDetailListCell reuseIdentifier]];
    if (!cell) {
        cell = [[CaoZuoDetailListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CaoZuoDetailListCell reuseIdentifier]];
    }
    RenYanGuanLiModel *model = self.dataAry[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.messageLb.text = model.content;
    cell.timeLb.text = model.addTime;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return [CaoZuoDetailListCell cellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

#pragma mark ----------------<#note#>----------------
- (NSMutableArray *)dataAry {
    if (!_dataAry) {
        self.dataAry = [@[] mutableCopy];
    }
    return _dataAry;
}

-(void)getCaoZuoJiLuList:(BOOL)isMore{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.model.ID  forKey:@"id"];
    [params setObject:@"20" forKey:@"rows"];
    [params setObject:[NSString stringWithFormat:@"%ld",self.pageIndex] forKey:@"page"];
    
    
    [MSNetworkManager postRevertUrl:[NSString stringWithFormat:@"%@list",CaoZuo_Url] params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        [self.tableView.mj_header endRefreshing];
        if (isSuccess) {
           
             NSArray *cacheArray = resonseObj[@"list"];
            if (isMore){
                if (cacheArray.count == 0){//没有更多数据
                    isMore?[self.tableView.mj_footer endRefreshing]:[self.tableView.mj_header endRefreshing];
                    return;
                } else {
              }
            } else {
                [self.dataAry removeAllObjects];
             }
            if (isMore == NO) {
                self.dataAry = [RenYanGuanLiModel  mj_objectArrayWithKeyValuesArray:cacheArray];
                [self.tableView.mj_header endRefreshing];
            } else {
                NSMutableArray *cacheMoreArray = [RenYanGuanLiModel  mj_objectArrayWithKeyValuesArray:cacheArray];
                [self.dataAry addObjectsFromArray:cacheMoreArray];
                [self.tableView.mj_footer endRefreshing];
            }
             [self.tableView reloadData];
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        
    } withShowHud:YES];
    
 

}

@end
